<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Array</title>
</head>

<body>

</body>
<script>
  var arr = [1, 23, 4, 3, 4];
  var sum = 0;
  var average
  //数组遍历
  arr.forEach(i => {
    sum += i
  })
  console.log("总和:", sum)
  average = sum / arr.length
  console.log("平均值", average)
  //取数组最值
  var s = arr.reduce((a, b) => {
    if (a > b) return a
    else return b
  })
  console.log("最大值:", s)
  //map操作
  var map = arr.map(i => i + 1);
  console.log(map)
  //数组分割
  var str = ['red', 'yellow', 'green', 'pink']
  var res = ''
  str.forEach(i => res += i + '!')
  console.log("字符串拼接:", res)

  //数组新增元素
  var str2 = ['red', 'yellow', 'green', 'pink']
  console.log("原始数组长度:", str2.length)
  str2.length = 5
  console.log("修改数组长度:", str2)
  str2.push("black")
  console.log("追加之后:", str2)
  var val = str2.pop();
  console.log("pop之后", str2, "pop=", val)

  //数组创建
  var arr2 = Array.from(Array(100), (v, k) => k + 1);
  console.log("1-100", arr2)

  //数组筛选
  var filterArr = arr2.filter(i => i % 2 == 0);
  console.log("偶数数组", filterArr)

  //反转数组
  var reverseArr = str.reverse();
  console.log(reverseArr)

  //排序
  filterArr.sort((a, b) => b - a);
  console.log("排序数组:", filterArr)
</script>

</html>